package com.goalias.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.goalias.domain.dto.DownloadDto;
import com.goalias.domain.entity.File;
import com.goalias.domain.vo.FileVo;
import com.goalias.exception.BizException;
import com.goalias.exception.ExceptionEnum;
import com.goalias.feign.domain.UserInfo;
import com.goalias.mapper.FileMapper;
import com.goalias.resp.Result;
import com.goalias.service.IFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.goalias.utils.IoUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 文件表 服务实现类
 * </p>
 *
 * @author goalias
 * @since 2024-12-18
 */
@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, File> implements IFileService {

    @Resource
    private IoUtil ioUtil;
    @Resource
    private FileMapper fileMapper;

    @Override
    public Result upload(MultipartFile file, Long categoryId) {
        String name = file.getOriginalFilename();
        String path;
        if (categoryId == 0) {
            path = ioUtil.USER_PATH();
        } else if (categoryId == -1) {
            path = ioUtil.OTHER_PATH();
        } else {
            path = ioUtil.CONTENT_PATH();
        }
        boolean isInsert = true;
        String fileName = path + name;
        try {
            if (ioUtil.isObjectExist(ioUtil.getBUCKET_NAME(), fileName)) {
                isInsert = false;
            }
            if (isInsert) {
                ioUtil.uploadFile(ioUtil.getBUCKET_NAME(), fileName, file.getInputStream());
                File entity = new File();
                entity.setFileName(fileName);
                entity.setFileSize(file.getSize());
                entity.setOpId(UserInfo.get().getId());
                entity.setCategoryId(categoryId);
                save(entity);
            }
            String url = ioUtil.getUrl(ioUtil.getBUCKET_NAME(), fileName);
            return Result.success(new FileVo(fileName, url));
        } catch (IOException e) {
            throw new BizException("上传文件失败");
        }
    }

    @Override
    public Result getUrl(String bucket, String fileName) {
        String reqBucket = StringUtils.hasText(bucket) ? bucket : ioUtil.getBUCKET_NAME();
        if (!ioUtil.isBucketExist(reqBucket)) {
            return Result.error(ExceptionEnum.FROM_NOT_EXIST);
        }
        String url = ioUtil.getUrl(reqBucket, fileName);
        if (!StringUtils.hasText(url)) {
            return Result.error(ExceptionEnum.RESOURCE_NOT_EXIST);
        }
        return Result.success(url);
    }

    @Override
    public Result delete(List<String> fileNames) {
        ioUtil.delFile(ioUtil.getBUCKET_NAME(), fileNames);
        QueryWrapper<File> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("file_name", fileNames);
        fileMapper.delete(queryWrapper);
        return Result.success();
    }
}
